package com.lindroy.gankmvp.http

import com.lindroy.gankmvp.api.GankService
import com.lindroy.gankmvp.constants.BASE_URL
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

/**
 * @author Lin
 * @date 2021/7/2
 * @function
 */

val gankService: GankService by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
    HttpUtil.create(GankService::class.java)
}

object HttpUtil {

    private val client = OkHttpClient.Builder().run {
       /* addInterceptor(
            LoggingInterceptor.Builder()
                .setLevel(Level.BASIC)
                .log(Platform.INFO)
                .tag("HttpRequest")
//                .addHeader("cityCode", "53")
//                .addQueryParam("moonStatus", "crescent")
                .build()
        )*/
        build()
    }
    private val retrofit = Retrofit.Builder()
        .baseUrl(BASE_URL)
        /* .client(OkHttpClient().newBuilder().addInterceptor { chain ->
             val request = chain.request()
             //获取请求方法和请求路径
 //            Logger.d("${request.method()}：${request.url()}")
             chain.proceed(request)
         }.build())*/
        .client(client)
        .addConverterFactory(GsonConverterFactory.create())//日志拦截器
        .build()

    fun <T> create(serviceClass: Class<T>): T = retrofit.create(serviceClass)

//    inline fun <reified T> create(): T = create(T::class.java)
}